"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValueByWeight = exports.getPrevPrev = exports.getRandomInt = void 0;
/**
 * 随机整数
 * @param min
 * @param max
 * @returns
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}
exports.getRandomInt = getRandomInt;
/**
 * 获取上上位
 * @param arr
 * @param num
 * @returns
 */
function getPrevPrev(arr, num) {
    const index = arr.indexOf(num);
    let prevIndex = index - 1;
    if (prevIndex < 0) {
        prevIndex = arr.length - 1;
    }
    let prevPrevIndex = prevIndex - 1;
    if (prevPrevIndex < 0) {
        prevPrevIndex = arr.length - 1;
    }
    return arr[prevPrevIndex];
}
exports.getPrevPrev = getPrevPrev;
function getValueByWeight(arr) {
    const totalWeight = arr.reduce((acc, cur) => acc + cur.weight, 0); // 总权重
    let randomNum = Math.random() * totalWeight; // 生成一个随机数
    for (let i = 0; i < arr.length; i++) {
        if (randomNum <= arr[i].weight) {
            return arr[i].value;
        }
        randomNum -= arr[i].weight;
    }
}
exports.getValueByWeight = getValueByWeight;
